package com.kingdee.pda.ui.sn_query_edit;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

import com.example.basecommon.utils.SpeakUtils;
import com.kingdee.pda.R;
import com.kingdee.pda.callback.MyTouchLinstener;
import com.kingdee.pda.entity.Warehouse;
import com.kingdee.pda.ui.activity.BaseTitleActivity;
import com.kingdee.pda.ui.activity.QueryLocationStockActivity;
import com.kingdee.pda.ui.popupwindow.ListSelectorPpWindow;
import com.kingdee.pda.ui.widget.CustomHScrollView;
import com.kingdee.pda.util.CommonUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 序列号查询
 * Created by dlj
 * DATE 2021/12/10
 * TIME 11:25
 */
public class SnQueryActivity extends BaseTitleActivity implements View.OnKeyListener, SnQueryContract.ISnQueryView {
    @BindView(R.id.et_skucode)
    EditText et_skucode;
    @BindView(R.id.et_snCode)
    EditText et_snCode;
    @BindView(R.id.et_ownerCode)
    EditText et_ownerCode;
    @BindView(R.id.et_status)
    EditText et_status;
    @BindView(R.id.h_scrollView)
    CustomHScrollView customHScrollView;
    @BindView(R.id.list_view)
    ListView mListView;
    private SnQueryListAdapter mAdapter;

    private List<Warehouse> orgList = new ArrayList<>();
    private List<SnStatus> statusList = new ArrayList<>();

    private Warehouse owner;
    private String status = "";
    private ListSelectorPpWindow<Warehouse> outOwnerSelector;
    private SnQueryPresenter snQueryPresenter;
    private SnStatus snStatusBean;
    private Warehouse ownerBean;

    public static void actionStart(Context context) {
        context.startActivity(new Intent(context, SnQueryActivity.class));
    }

    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        super.onActivityCreate(savedInstanceState);
        setContentView(R.layout.activity_sn_query);

        statusList.add(new SnStatus("", "全部"));
        statusList.addAll(CommonUtil.getSnStatusList());
    }

    @Override
    protected void onResume() {
        super.onResume();
        mAdapter.setData(new ArrayList<>());
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("序列号查询编辑");

        setEditTextScannerEvents(et_skucode, this);
        setEditTextScannerEvents(et_snCode, this);

        mListView.setOnTouchListener(new MyTouchLinstener(customHScrollView));

        mAdapter = new SnQueryListAdapter(this, customHScrollView);
        mListView.setAdapter(mAdapter);

        snQueryPresenter = new SnQueryPresenter(this, this);
        snQueryPresenter.getOrgList();
    }

    @Override
    public void getOrgListSuccess(List<Warehouse> warehouseList) {
        orgList.clear();
        orgList.add(new Warehouse(0, "全部"));
        for (Warehouse org : warehouseList) {
            if (org.isFisaccounting()) {
                orgList.add(org);
            }
        }
    }

    @Override
    public void pdaSnQuerySuccess(List<ItemSNInfoVo> snInfoVos) {
        if (snInfoVos == null || snInfoVos.size() == 0) {
            SpeakUtils.speakWithToast(SnQueryActivity.this, "未查询到序列号信息，请确认查询数据！", true, false);
            return;
        }
        mAdapter.setData(snInfoVos);
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (!(v instanceof EditText)) {
            return false;
        }
        EditText editText = (EditText) v;
        String input = editText.getText().toString().trim();
        if ((keyCode == KeyEvent.KEYCODE_SYSRQ || keyCode == KeyEvent.KEYCODE_ENTER) && event.getAction() == KeyEvent.ACTION_UP) {
            switch (v.getId()) {
                case R.id.et_skucode:
                    if (!TextUtils.isEmpty(input)) {
                        input = CommonUtil.skuCodeSplit(SnQueryActivity.this, input);
                        if (TextUtils.isEmpty(input)) {
                            return false;
                        }
                        et_skucode.setText(input);
                    }
                    break;
            }
            return true;
        }
        return false;
    }

    @Override
    public void changeWarehouse(Warehouse warehouse) {
    }

    @OnClick({R.id.btn_query, R.id.iv_ownercode_select, R.id.iv_status_select})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_query:
                String skucode = et_skucode.getText().toString().trim();
                String snCode = et_snCode.getText().toString().trim();
                if (TextUtils.isEmpty(skucode) && TextUtils.isEmpty(snCode) && ownerBean == null && snStatusBean == null) {
                    SpeakUtils.speakWithToast(SnQueryActivity.this, "查询条件至少输入一个，再进行查询！", true, false);
                } else {

                    HashMap<String, Object> map = new HashMap<>();
                    map.put("itembarcode", TextUtils.isEmpty(skucode) ? "" : skucode);
                    map.put("snno", TextUtils.isEmpty(snCode) ? "" : snCode);
                    map.put("snStatus", snStatusBean == null ? "" : snStatusBean.getStatus());
                    map.put("owner", owner == null ? "" : owner.getId());
                    map.put("warehouse", getCurrentWarehouse().getId());

                    snQueryPresenter.pdaSnQuery(map);
                }
                break;
            case R.id.iv_ownercode_select:
                if (orgList != null)
                    showoutOwnerSelector(orgList);
                break;
            case R.id.iv_status_select:
                showSnStatusSelector();
                break;
            default:
                break;
        }
    }

    /**
     * 展示调出货主选择框
     */
    private void showoutOwnerSelector(List<Warehouse> list) {
        if (outOwnerSelector != null) outOwnerSelector.dismiss();
        outOwnerSelector = new ListSelectorPpWindow<Warehouse>(SnQueryActivity.this,
                R.drawable.shape_bg_edit_selected,
                et_skucode.getWidth(),
                list, new ListSelectorPpWindow.onItemSelectedListener() {
            @Override
            public void onItemSelected(Object item) {
                ownerBean = (Warehouse) item;
                et_ownerCode.setText(ownerBean.getName());
                if ("全部".equals(ownerBean.getName())) {
                    owner = null;
                } else {
                    owner = ownerBean;
                }
            }
        });
        outOwnerSelector.showAsDropDown(et_ownerCode, 0, 10);
    }

    /**
     * 展示状态选择框
     */
    private void showSnStatusSelector() {
        ListSelectorPpWindow<SnStatus> locationSelector = new ListSelectorPpWindow<SnStatus>(SnQueryActivity.this,
                R.drawable.shape_bg_edit_selected,
                et_status.getWidth(),
                statusList, new ListSelectorPpWindow.onItemSelectedListener() {
            @Override
            public void onItemSelected(Object item) {
                snStatusBean = (SnStatus) item;
                et_status.setText(snStatusBean.getDes());
            }
        });
        locationSelector.showAsDropDown(et_status, 0, 10);
    }
}
